package com.vince.hulk.character
{
import org.flixel.FlxObject;

/**
 * author yangwz
 */		
public class SmallCharacter extends Character
{
	//==========================================================================
	//  Constructor
	//==========================================================================
	/**
	 * Constructs a new <code>SmallCharacter</code> instance.
	 * 
	 */
	public function SmallCharacter(X:Number=0, Y:Number=0)
	{
		super(X, Y, null);
		loadGraphic(blobSmallImg, true, true, 41, 40);
		width = 29;
		offset.x = 6;
		addAnimation("walk", [1, 2, 3, 4], 8, true);
		addAnimation("idle", [0, 9], 4, true);
		addAnimation("jump", [3, 5], 6, false);
		addAnimation("noMerge", [8, 7], 3, true);
		_moveSpeed = 300;
		_jumpPower = 500;
		noMerge = 0;
		walking = 0;
	}
	//==========================================================================
	//  Variables
	//==========================================================================
	[Embed(source = "assets/HulkSmall.png")]
	private var blobSmallImg:Class;
	
	private var noMerge:int;
	
	private var walking:int;
	//==========================================================================
	//  Methods
	//==========================================================================
	override public function merge(character:Character):Character
	{
		var _character:Character = null;
		if (character.height > height)
		{
			_character = new LargeCharacter(x, y);
		}
		else
		{
			_character = new MediumCharacter(x, y);
		}
		_character.x = x - width - 5;
		_character.y = y + height - _character.height;
		if (isOverlapping(_character))
		{
			_character.x = x;
			if (isOverlapping(_character))
			{
				_character.y = y + height;
				if (isOverlapping(_character))
				{
					_character.kill();
					setCouldntMerge();
					character.setCouldntMerge();
					return null;
				}
			}
		}
		character.kill();
		kill();
		return _character;
	}
	
	override public function split():Array
	{
		var result:Array = new Array();
		result.push(this);
		return result;
	}
	
	override public function setCouldntMerge():void
	{
		noMerge = 80;
	}
	
	override public function update():void
	{
		if (velocity.x == 0)
		{
			this.walking = 0;
		}
		if (this.noMerge > 0)
		{
			play("noMerge");
			var character:SmallCharacter = this;
			var merge:int = noMerge - 1;
			character.noMerge = merge;
		}
		else if (velocity.x != 0 && isTouching(FlxObject.DOWN))
		{
			play("walk");
		}
		else if (!isTouching(FlxObject.DOWN))
		{
			play("jump");
		}
		else
		{
			play("idle");
		}
		super.update();
	}
}
}